import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_order_item.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class MyOrderListPage extends StatefulWidget {
  const MyOrderListPage({Key? key}) : super(key: key);

  @override
  State<MyOrderListPage> createState() => _MyOrderListPageState();
}

class _MyOrderListPageState extends State<MyOrderListPage> {
  final controller = Get.find<MyOrderListController>();
  final state = Get.find<MyOrderListController>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: MyOrderListController(),
        builder: (_) {
          return MkPage(
            child: Scaffold(
              backgroundColor: Colors.transparent,
              appBar: AppBar(
                title: Text('我的订单'),
                bottom: PreferredSize(
                  preferredSize: Size.fromHeight(90),
                  child: Padding(
                    padding: EdgeInsets.all(AppTheme.iDefaultPadding),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        _buildAnalysisCard("订单数", Assets.myOrderOrder,
                            Color(0xFFFFF3F0), Color(0xFFFF8362)),
                        _buildAnalysisCard("实付金额", Assets.myOrderSfje,
                            Color(0xFFEAEFF9), Color(0xFF5B8FF9)),
                        _buildAnalysisCard("优惠金额", Assets.myOrderYhje,
                            Color(0xFFE9F5F1), Color(0xFF5AD8A6)),
                      ],
                    ),
                  ),
                ),
              ),
              body: Obx(() => _buildBody()),
            ),
          );
        });
  }

  _buildAnalysisCard(title, icon, bgColor, textColor) {
    return Container(
      width: (Get.width - AppTheme.iDefaultPadding * 4) / 3,
      height: 80,
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultPadding),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('${title}', style: TextStyle(fontSize: 14)),
              MkLoadImage(icon, width: 14)
            ],
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('1544',
                  style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: textColor)),
              Gaps.hGap4,
              Text('单'),
            ],
          )
        ],
      ),
    );
  }

  _buildBody() {
    return EasyRefresh(
      // 下拉样式
      header: TDRefreshHeader(),
      footer: TaurusFooter(),
      child: ListView.builder(
        itemBuilder: (context, index) {
          return AppOrderItem(orderDto: state.orderList[index]);
        },
        itemCount: state.orderList.length,
      ),
      // 下拉刷新回调
      onRefresh: controller.onRefresh,
      onLoad: controller.onLoad,
    );
  }

  @override
  void dispose() {
    Get.delete<MyOrderListController>();
    super.dispose();
  }
}
